library(tidyverse)
library(p8105.datasets)
library(plotly)
data("ny_noaa")

ny_noaa = 
  ny_noaa |>
  drop_na() |>
  mutate(
    tmax = as.numeric(tmax),
    tmin = as.numeric(tmin)
  ) |>
  mutate(year = year(date))

Plotly plots

Scatterplot

ny_noaa |>
  filter(year == 2002, month(date) %in% 6:9) |> 
  plot_ly(x = ~tmin, y = ~tmax, text = ~date, color = ~factor(month(date)), 
          alpha = .5, type = "scatter", mode = "markers", colors = "viridis") |>
  layout(title = "tmax vs tmin (June to September 2002)",
         xaxis = list(title = "tmax"),
         yaxis = list(title = "tmin"),
         coloraxis = list(title = "Month"))

Boxplot

ny_noaa |>
  plot_ly(y = ~tmax, type = "box", name = "tmax") |>
  add_trace(y = ~tmin, type = "box", name = "tmin") |>
  layout(title = "tmax and tmin Distribution",
         yaxis = list(title = "Temperature"))

Bar plot

ny_noaa |>
  filter(year %in% 2001:2010) |>
  group_by(year) |>
  summarize(avg_snow = mean(snow, na.rm = TRUE)) |>
  plot_ly(x = ~year, y = ~avg_snow, type = 'bar', name = 'Snowfall') |>
  layout(title = "Average Snowfall (2001-2010)",
         xaxis = list(title = "Year"),
         yaxis = list(title = "Average Snowfall"))